
package net.biaji.android.alephclient;

import net.biaji.alephclient.Book;
import net.biaji.alephclient.LibAgent;
import net.biaji.android.alephclient.data.DataContract;
import net.biaji.android.alephclient.data.DataFusionManager;
import net.biaji.android.alephclient.data.DataContract.Books;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class BookDetailFragment extends Fragment {

    public static final String KEY_DOC_NUMBER = "DOC_NUMBER";

    private String docNumber;

    private ImageView cover;

    private TextView bookTitle, authors, publisher, isbn, publishDate;

    private ProgressBar progress;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.book_detail, null);
        cover = (ImageView) view.findViewById(R.id.bookCover);
        bookTitle = (TextView) view.findViewById(R.id.bookTitle);
        authors = (TextView) view.findViewById(R.id.authors);
        publisher = (TextView) view.findViewById(R.id.publisher);
        isbn = (TextView) view.findViewById(R.id.isbn);
        publishDate = (TextView) view.findViewById(R.id.publish_date);
        progress = (ProgressBar) view.findViewById(R.id.progress);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        load();
    }

    @Override
    public void setArguments(Bundle args) {
        this.docNumber = (String) args.get(KEY_DOC_NUMBER);
    }

    private void load() {

        new AsyncTask<String, Void, Book>() {

            @Override
            protected void onPreExecute() {
                progress.setVisibility(View.VISIBLE);
            }

            @Override
            protected Book doInBackground(String... params) {
                if (params == null || params.length < 1)
                    return null;

                String sn = params[0];

                Book result = null;
                if (DataFusionManager.getInstance(getActivity()).getBookStatus(sn) == Books.BOOK_STATUS_NORMAL) {
                    result = DataFusionManager.getInstance(getActivity()).getBook(sn);
                    return result;
                }

                LibAgent agent = AgentSingleton.getInstance();
                result = agent.getBook(params[0]);
                return result;
            }

            @Override
            protected void onPostExecute(Book result) {
                if (result != null) {
                    progress.setVisibility(View.GONE);
                    bookTitle.setText(result.getName());
                    authors.setText(result.getAuthorStr());
                    publisher.setText(result.getPublisher());
                    publishDate.setText(result.getPubDate());
                    isbn.setText(result.getISBN());
                    DataFusionManager.getInstance(getActivity()).putBook(result,
                            DataContract.Books.BOOK_STATUS_NORMAL);

                }
            }

        }.execute(docNumber);

    }

}
